<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>门票管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">

</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">门票管理</div>
                <div class="layui-card-body">

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-btn-group test-table-operate-btn">

                                <button class="layui-btn" data-type="addadmin">添加门票</button>

                            </div>
                        </div>
                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate">

                        <script type="text/html" id="is_use_switch">
                            <input type="checkbox" name="is_use" lay-skin="switch" lay-text="上架|下架" lay-filter="is_use_switch"
                                   value="{{ d.is_use }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.is_use == 1 ? 'checked' : '' }}>
                        </script>

                        <script type="text/html" id="is_discount_switch">
                            <input type="checkbox" name="is_discount" lay-skin="switch" lay-text="是|否" lay-filter="is_discount_switch"
                                   value="{{ d.is_discount }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.is_discount == 1 ? 'checked' : '' }}>
                        </script>

                        <script type="text/html" id="can_refund_switch">
                            <input type="checkbox" name="can_refund" lay-skin="switch" lay-text="是|否" lay-filter="can_refund_switch"
                                   value="{{ d.can_refund }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.can_refund == 1 ? 'checked' : '' }}>
                        </script>

                    </table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<!--添加弹窗-->
<div id="addmodal" style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">门票名称</label>
            <div class="layui-input-block">
                <input type="text" name="tname" lay-verify="required" placeholder="请输入门票名" autocomplete="off" class="layui-input">
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label">门票价格</label>
            <div class="layui-input-block">
                <input type="text" name="price" lay-verify="required|number" placeholder="门票价格" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">是否优惠</label>
            <div class="layui-input-block">
                <input type="checkbox" name="is_discount" lay-filter="add_discount" lay-skin="switch" lay-text="是|否">
            </div>
        </div>

        <div class="layui-form-item" id="discount_tag_div" style="display: none;">
            <label class="layui-form-label">优惠标签</label>
            <div class="layui-input-block">
                <input type="text" name="discount_name_tag" placeholder="一个优惠的标签" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" id="discount_price_div" style="display: none;">
            <label class="layui-form-label">优惠价格</label>
            <div class="layui-input-block">
                <input type="text" name="discount_price" placeholder="门票优惠价格" autocomplete="off" class="layui-input">
            </div>
        </div>

        <!--<div class="layui-form-item">-->
            <!--<label class="layui-form-label">可退款</label>-->
            <!--<div class="layui-input-block">-->
                <!--<input type="radio" name="can_refund" value="1" title="是" checked>-->
                <!--<input type="radio" name="can_refund" value="0" title="否">-->
            <!--</div>-->
        <!--</div>-->

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">有效期</label>
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="start_time" id="add_start_time" lay-verify="required" readonly placeholder="开始时间" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="end_time" id="add_end_time" lay-verify="required" readonly placeholder="结束时间" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>


        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addsubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>
</div>

<!--修改弹窗-->
<div id="editmodal" style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">门票名称</label>
            <div class="layui-input-block">
                <input type="text" name="tname" id="edit_tname" lay-verify="required" placeholder="请输入门票名" autocomplete="off" class="layui-input">
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label">门票价格</label>
            <div class="layui-input-block">
                <input type="text" name="price" id="edit_price" lay-verify="required|number" placeholder="门票价格" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" id="discount_tag_div">
            <label class="layui-form-label">优惠标签</label>
            <div class="layui-input-block">
                <input type="text" name="discount_name_tag" id="edit_discount_name_tag" placeholder="一个优惠的标签" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" id="discount_price_div">
            <label class="layui-form-label">优惠价格</label>
            <div class="layui-input-block">
                <input type="text" name="discount_price" id="edit_discount_price" placeholder="门票优惠价格" autocomplete="off" class="layui-input">
            </div>
        </div>

        <!--<div class="layui-form-item">-->
            <!--<label class="layui-form-label">可退款</label>-->
            <!--<div class="layui-input-block">-->
                <!--<input type="radio" id="edit_can_refund_yes" name="can_refund" value="1" title="是" checked>-->
                <!--<input type="radio" id="edit_can_refund_no" name="can_refund" value="0" title="否">-->
            <!--</div>-->
        <!--</div>-->

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">有效期</label>
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="start_time" id="edit_start_time" lay-verify="required" readonly placeholder="开始时间" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 150px;">
                    <input type="text" name="end_time" id="edit_end_time" lay-verify="required" readonly placeholder="结束时间" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <input type="hidden" id="edit_tid" name="tid">

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editsubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        //执行一个laydate实例
        laydate.render({
            elem: '#add_start_time' //指定元素
        });

        laydate.render({
            elem: '#add_end_time' //指定元素
        });

        laydate.render({
            elem: '#edit_start_time' //指定元素
        });

        laydate.render({
            elem: '#edit_end_time' //指定元素
        });

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Ticket/ticket')}"
            ,height: 'full-155'
            ,cellMinWidth:100
            ,cols: [[
                 {field:'tname',align:'center', title: '门票名称'}
                ,{field:'price',width:100, align:'center', title: '价格'}
                ,{field:'is_discount',align:'center',width:100,  title: '是否优惠',templet:'#is_discount_switch'}
                ,{field:'discount_price',align:'center', width:100,title: '优惠价格'}
                // ,{field:'can_refund',align:'center',width:100, title: '是否可退',templet: '#can_refund_switch'}
                ,{field:'is_use',width:100, align:'center', title: '是否上架',templet: '#is_use_switch'}
                //,{field:'sort',width:130,  title: '权重(点击修改)',edit:'text'}
                ,{field:'time',width:200, align:'cneter', title: '门票有效期',templet:function(d){
                    return d.start_time +'到'+d.end_time;
                    }}
                ,{field:'create_time',width:180, align:'center', title: '创建时间'}
                ,{field:'admin_rename',width:120, align:'center', title: '创建人'}
                ,{width:150, align:'center',title: '操作',fixed: 'right', toolbar: '#test-table-operate-barDemo'}
            ]]
        });

        form.render(null, 'component-form-group');

        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
               layer.confirm('确认删除'+data.tname+'吗', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "{:url('Ticket/delTicket')}",
                        type: 'post',
                        dataType: 'json',
                        data: {id:data.tid},
                        success: function(msg){
                            layer.close(loading);
                            if (msg.code === 0) {
                                layer.msg('删除成功',{icon:2,time:1000},function(){
                                    obj.del();
                                    layer.close(index);
                                });

                            }else if (msg.code === 1){
                                layer.msg('删除失败',{icon:2,time:1000});
                            }
                        }
                    })
                });
            } else if(obj.event === 'edit'){                           //修改操作

                $('#edit_tid').val(data.tid);
                $('#edit_tname').val(data.tname);
                $('#edit_category_id').val(data.category_id);
                $('#edit_price').val(data.price);
                $('#edit_discount_price').val(data.discount_price);
                $('#edit_discount_name_tag').val(data.discount_name_tag);
                $('#edit_start_time').val(data.start_time);
                $('#edit_end_time').val(data.end_time);
                if (data.can_refund == 1){

                    $("#edit_can_refund_yes").prop("checked",true);
                    $("#edit_can_refund_no").prop("checked",false);

                }else{

                    $("#edit_can_refund_no").prop("checked",true);
                    $("#edit_can_refund_yes").prop("checked",false);

                }

                form.render();

                layer.open({
                     title:'修改门票信息'
                    ,type: 1
                    ,shadeClose: true
                    ,area: ['700px']
                    ,content: $('#editmodal')
                });
            }
        });

        //添加提交
        form.on('submit(addsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            if(data.field.is_discount !== 'on'){
                data.field.is_discount = 'off'
            }

            $.ajax({
                url: "{:url('Ticket/addTicket')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(data){
                    if (data.code === 0) {
                        layer.msg('添加成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.code,{icon:2,time:1000})
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //修改提交
        form.on('submit(editsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            $.ajax({
                url: "{:url('Ticket/editTicket')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(data){

                    if (data.code === 0) {
                        layer.msg('修改成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.code,{icon:2,time:1000})
                    }
                }

            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //开关修改是否上架
        form.on('switch(is_use_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            var is_use = this.checked ? '1' : '0';

            $.ajax({
                url: "{:url('Ticket/editTicketIsUse')}",
                type: 'post',
                dataType: 'json',
                data: {tid:json.tid,is_use:is_use},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg('修改失败',{icon:2,time:1000},function(){
                            //layer.close(loading);
                            window.location.reload();
                        });
                    }
                }
            })
        });

        //开关修改是否可退
        form.on('switch(can_refund_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            var can_refund = this.checked ? '1' : '0';

            $.ajax({
                url: "{:url('Ticket/editTicketCanRefund')}",
                type: 'post',
                dataType: 'json',
                data: {tid:json.tid,can_refund:can_refund},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg('修改失败',{icon:2,time:1000},function(){
                            window.location.reload();
                        });
                    }
                }
            })
        });

        //开关修改是否优惠
        form.on('switch(is_discount_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            var is_discount = this.checked ? '1' : '0';

            $.ajax({
                url: "{:url('Ticket/editTicketIsDiscount')}",
                type: 'post',
                dataType: 'json',
                data: {tid:json.tid,is_discount:is_discount},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg('修改失败',{icon:2,time:1000},function(){
                            window.location.reload();
                        });
                    }
                }
            })
        });

        form.on('switch(add_discount)', function(data){
            
            if (data.elem.checked){

                $('#discount_tag_div').fadeIn();
                $('#discount_price_div').fadeIn();

            }else{

                $('#discount_tag_div').fadeOut();
                $('#discount_price_div').fadeOut();

            }

        });

        //监听单元格编辑
        table.on('edit(test-table-operate)', function(obj) {
            var reg = /^[1-9]+[0-9]*]*$/;

            if (!reg.test(obj.value)){
                layer.msg('请输入正整数',{time:1000},function(){
                    table.reload('test-table-operate');
                });
                return false;
            }

            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            $.ajax({
                url: "{:url('Ticket/editTicketSort')}",
                type: 'post',
                dataType: 'json',
                data: {tid:obj.data.tid,sort:obj.value},
                success: function(msg){
                    layer.closeAll();
                    layer.msg('修改成功',{time:1000},function(){
                        table.reload('test-table-operate');
                    });
                }
            })

        });

        var $ = layui.$, active = {
            addadmin: function() {
                var addmodal = layer.open({
                    title: '添加门票'
                    , type: 1
                    , shadeClose: true
                    , area: ['700px','620px']
                    , content: $('#addmodal')
                });
            }

        };

        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>


</body>
</html>